package com.hethongviet.preventing.page;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.hethongviet.preventingmachine.AppMain;
import com.hethongviet.preventingmachine.Constants;
import com.hethongviet.preventingmachine.R;
import com.hethongviet.preventingmachine.Utils;
import com.hethongviet.preventingmachine.helps.CommonUtils;
import com.hethongviet.preventingmachine.helps.Device;
import com.hethongviet.preventingmachine.helps.DeviceSQLite;
import com.hethongviet.preventingmachine.helps.dialog.DialogMessageBack;
import com.hethongviet.preventingmachine.helps.OnSMSMessageListener;
import com.hethongviet.preventingmachine.helps.SMSHelperDoor;
import com.hethongviet.preventingmachine.helps.SmsReceiverDoor;
import com.hethongviet.preventingmachine.models.SMS;

/** Page is used to handle changing pincode */
public class PageChangePincodeActivity extends Activity implements
		OnSMSMessageListener
{
	private String _newPassword = null;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);

		Utils.prepareWindow(this);

		SMSHelperDoor.setActivity(this);

		setContentView(R.layout.page_change_password);

		final String oldPassword = getIntent().getStringExtra(
				Constants.EXTRA_PASSWORD);

		Utils.debug("oldPassword = " + oldPassword);

		final EditText editTextOldPassword = (EditText) findViewById(R.id.change_password_edit_old_password);
		final EditText editTextNewPassword = (EditText) findViewById(R.id.change_password_edit_new_password);
		final EditText editTextReinputPassword = (EditText) findViewById(R.id.change_password_edit_reinput_password);

		final Button buttonAccept = (Button) findViewById(R.id.change_button_accept);
		final Button buttonCancel = (Button) findViewById(R.id.change_button_cancel);

		buttonAccept.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View arg0)
			{
				String oldPasswordReinput = editTextOldPassword.getText().toString();

				Utils.debug("oldPassword = " + oldPassword);
				Utils.debug("oldPasswordReinput = " + oldPasswordReinput);

				if (TextUtils.isEmpty(oldPasswordReinput)
						|| !oldPasswordReinput.equals(oldPassword))
				{
					showDialogWithCatch(R.string.message_error_input_password_old_not_correct);
					return;
				}

				for (int index = 0; index < oldPasswordReinput.length(); index++)
				{
					char c = oldPasswordReinput.charAt(index);

					if (!Character.isLetterOrDigit(c))
					{
						showDialogWithCatch(R.string.message_error_wrong_input_password);
						return;
					}
				}

				_newPassword = editTextNewPassword.getText().toString();
				if (_newPassword == null || _newPassword.equals(""))
				{
					editTextNewPassword.setText("");
					showDialogWithCatch(R.string.message_error_input_null);
					return;
				}

				for (int index = 0; index < _newPassword.length(); index++)
				{
					char c = _newPassword.charAt(index);

					if (!Character.isLetterOrDigit(c))
					{
						showDialogWithCatch(R.string.message_error_wrong_input_password);
						return;
					}
				}

				if (_newPassword.length() < Constants.LENGTH_MIN_NAME)
				{
					showDialogWithCatch(R.string.message_error_input_too_short_password);
					return;
				}

				if (_newPassword.length() > Constants.LENGTH_MAX_NAME)
				{
					showDialogWithCatch(R.string.message_error_input_too_long_password);
					return;
				}

				if (_newPassword.equals(oldPassword))
				{
					showDialogWithCatch(R.string.message_error_input_password_same_old);
					return;
				}

				String reinputPassword = editTextReinputPassword.getText().toString();
				if (reinputPassword == null
						|| !reinputPassword.equals(_newPassword))
				{
					showDialog(R.string.message_error_input_password_reinput_not_same);
					return;
				}

				/* Send SMS change user_name */
				Utils.showProgress(PageChangePincodeActivity.this);

				SMSHelperDoor.changePassword(_newPassword);
			}
		});
		buttonCancel.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View arg0)
			{
				setResult(RESULT_CANCELED);
				finish();
			}
		});
	}

	@Override
	public void onSMSMessageIncoming(SMS sms)
	{
		String message = CommonUtils.prepareMessage(sms.getMessage());
		Utils.debug("Message receive = " + message);

		setResult(RESULT_OK);

		DeviceSQLite deviceSQLite = new DeviceSQLite(this,
				AppMain.getInstance().getDBName());
		Device device = deviceSQLite.getAll().get(0);
		device.setPin(_newPassword);
		deviceSQLite.insert(device);

		SMSHelperDoor.setPassword(_newPassword);

		showDialogWithCatch(R.string.message_success_change_password);

		Utils.hideProgressDialog();
	}

	@Override
	protected void onResume()
	{

		SmsReceiverDoor.setOnSMSMessageListener(this);
		SMSHelperDoor.setActivity(this);

		super.onResume();
	}

	private void showDialogWithCatch(int id)
	{
		try
		{
			showDialog(id);
		} catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	@Override
	protected Dialog onCreateDialog(int id)
	{
		if (id == R.string.message_error_change_password_fail)
		{
			return new DialogMessageBack(this, true, id, true);
		}
		else if (id == R.string.message_success_change_password)
		{
			return new DialogMessageBack(this, false, id, true);
		}
		else if (id == R.string.message_error_input_too_long_password)
		{
			return new DialogMessageBack(this, true, String.format(
					getString(id), Constants.LENGTH_MAX_PASSWORD), false);
		}
		else if (id == R.string.message_error_input_too_short_password)
		{
			return new DialogMessageBack(this, true, String.format(
					getString(id), Constants.LENGTH_MIN_PASSWORD), false);
		}
		else
			return new DialogMessageBack(this, true, id, false);
	}

}
